package com.ruoyi.buss.schedule.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.buss.dxts.domain.DataCustDxts;
import com.ruoyi.buss.dxts.mapper.DataCustDxtsMapper;
import com.ruoyi.buss.schedule.service.ScheduleService;
import com.ruoyi.buss.system.domain.SysBusparams;
import com.ruoyi.buss.system.mapper.SysBusparamsMapper;
import com.ruoyi.buss.utils.GetOnlyId;
import com.ruoyi.buss.utils.Params;
import com.ruoyi.buss.utils.ShortMessageServiceSupport;
@Service
public class ScheduleServiceImpl implements ScheduleService{
	@Autowired
	private SysBusparamsMapper sysBusparamsMapper;
	
	@Autowired
    private DataCustDxtsMapper dataCustDxtsMapper;
	@Transactional
	public void smsSend() {
		 /**
		 * 判断当前配置权限是否查询所有客户
		 */
		SysBusparams sysBusparams = sysBusparamsMapper.selectSysBusparamsById(Params.IF_QUERY_DX);
		String ifCheck = sysBusparams.getValue();
		if("0".equals(ifCheck)) {
			 return;
		}
		System.out.println(">>>>>>>>>>>>开始发送短信");
		//删除发送失败的短信
		String status = "0";
		dataCustDxtsMapper.deleteDataCustDxtsByStatus(status);
		//获取未分配客户的信息
		//只给支行行长发送短信  总行任务仅限hznsyh
		List<Map<String, String>> lists = dataCustDxtsMapper.getwfpkh();
		for (Map<String, String> maps : lists) {
			System.out.println("当前循环:"+maps);
			List<Map<String, String>> list = dataCustDxtsMapper.getwfpkhDx(maps);
			if(list != null &&list.size()>0) {
				Map<String, String> timeMap = list.get(0);
				String time = timeMap.get("time");
				 int a = Integer.valueOf(time);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			    Date date = new Date();
			    String newTime = sdf.format(new Date(date.getTime() - 1000*60*60*24*3L));
			    int b = Integer.valueOf(newTime);
			    System.out.println(">>>>>>>:上次发送短信日期:"+a+",当前日期:"+b);
			    if(b<a) {
			    	System.out.println("跳出本次循环");
			    	continue;
			    }
			}
			
			Map<String, String> map = new HashMap<String, String>();
			map.putAll(maps);
			status = "1";
			// 发送短信
			String msg = "尊敬的【name】行长,您好!您的支行名下【taskName】任务存在未分配管户的客户,请及时处理,如非本支行任务,请忽略此短信。<效能管理系统>";
			msg = msg.replaceAll("【name】", map.get("name")==null?"":map.get("name"));
			msg = msg.replaceAll("【taskName】",map.get("taskName")==null?"":map.get("taskName"));
			Map<String, Object> result = new ShortMessageServiceSupport().sendShortMessage(map.get("phone")==null?"13401837144":map.get("phone"), msg);
			boolean flag = (boolean) result.get("success");
			if (!flag) {
				status = "0";
			}

			DataCustDxts dataCustDxts = new DataCustDxts();
			String id = GetOnlyId.getOnlyId();
			dataCustDxts.setId(id);
			dataCustDxts.setDxnr(msg);
			dataCustDxts.setStatus(status);
			
			dataCustDxts.setTaskName(maps.get("taskName"));
			dataCustDxts.setZhUserPhone(maps.get("phone"));
			dataCustDxts.setTaskId(maps.get("taskId"));
			dataCustDxts.setZhUserId(maps.get("userId"));
			dataCustDxts.setZhUserName(maps.get("name"));
			dataCustDxtsMapper.insertDataCustDxts(dataCustDxts);
		}
	}
}
